﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;

using DotNetNuke.Entities.Portals;


namespace DotNetNuke.Modules.UserDefinedTable
{


  public class EditCurrency : EditString
  {



    //public EditCurrency()
    //{
    //  base.Init += new EventHandler(this.EditCurrency_Init);
    //}


    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      //base.Init += new EventHandler(this.EditCurrency_Init);

      base.ctlValueBox.Attributes.Add("style", "text-align:right");
      Unit unit2 = new Unit("10em");
      base.ctlValueBox.Width = unit2;
      Label label2 = new Label();
      label2.CssClass = "Normal";
      Label child = label2;
      
      if ( string.IsNullOrEmpty(this.OutputSettings) )
        child.Text = string.Format("&nbsp;{0}", PortalController.GetCurrentPortalSettings().Currency);
      
      else
        child.Text = string.Format("&nbsp;{0}", this.OutputSettings);
      
      this.Controls.Add(child);

    }





    //private void EditCurrency_Init(object sender, EventArgs evt)
    //{
    //  base.ctlValueBox.Attributes.Add("style", "text-align:right");
    //  Unit unit2 = new Unit("10em");
    //  base.ctlValueBox.Width = unit2;
    //  Label label2 = new Label();
    //  label2.CssClass = "Normal";
    //  Label child = label2;
    //  if (this.OutputSettings == string.Empty)
    //  {
    //    child.Text = string.Format("&nbsp;{0}", PortalController.GetCurrentPortalSettings().Currency);
    //  }
    //  else
    //  {
    //    child.Text = string.Format("&nbsp;{0}", this.OutputSettings);
    //  }
    //  this.Controls.Add(child);
    //}




    protected override bool IsValidType()
    {
      decimal num;
      
      if (!string.IsNullOrEmpty(this.Value) && !decimal.TryParse(this.Value, out num))
        return false;
      
      return true;
    }




    public override string Value
    {
      get
      {
        string str;
        try
        {
          str = string.Empty;
          CultureInfo provider = new CultureInfo("en-US");
          if (base.Value != "")
            str = decimal.Parse(base.Value).ToString(provider);

        }
        catch (Exception e)
        {

          str = base.Value;

        }
        return str;
      }
      set
      {
        CultureInfo provider = new CultureInfo("en-US");
        if (value != "")
          value = decimal.Parse(value, provider).ToString("#,###,##0.00");

        base.Value = value;
      }
    }



  }

}

